package auth

import (
	"github.com/gin-gonic/gin"
	"wskyygyso/api/app/http/controllers/api/v1"
	"wskyygyso/api/app/models/user"
	"wskyygyso/api/app/requests"
	"wskyygyso/api/pkg/response"
)

type PassWordController struct {
	v1.BaseApiController
}

func (receiver *PassWordController) ResetPhone(c *gin.Context) {
	request := requests.ResetPasswordByPhoneRequest{}
	if !requests.Validate(c, &request, requests.ResetPassWordByPhone) {
		return
	}

	userModel := user.GetByPhone(request.Phone)
	if userModel.ID == 0 {
		response.Abort404(c, "账号不存在")
	} else {
		userModel.PassWord = request.PassWord
		userModel.Save()
		response.SUCCESS(c)
	}
}

func (receiver *PassWordController) ResetEmail(c *gin.Context) {
	request := requests.ResetPassWordByEmailRequest{}
	if !requests.Validate(c, &request, requests.ResetPassWordByEmail) {
		return
	}

	userModel := user.GetByMulti(request.Email)
	if userModel.ID == 0 {
		response.Abort404(c, "账号不存在")
	} else {
		userModel.PassWord = request.PassWord
		userModel.Save()
		response.SUCCESS(c)
	}
}
